package servlet.request;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-07-16
 * Time:11:59
 */
@WebServlet(name = "CheckServlet", urlPatterns = "/checkServlet")
public class CheckServlet extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        if (username.equals("admin")) {
            req.setAttribute("user","管理员");
        } else {
            req.setAttribute("user","普通用户");
        }
        /*
        /manageServlet是要转发的Servlet的URL
        '/' 会被解析为/Servlet/
        forward(req, resp)表示把当前Servlet的request对象和response对象传递给下一个Servlet    通过Tomcat
         */
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/manageServlet");
        requestDispatcher.forward(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
